package resource.data;

import java.sql.SQLException;
import java.util.List;

import app.MyApplication;

import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;

public class OrmliteDataContext implements IDataContext {
	private OrmliteSession session;

	public OrmliteDataContext() {
		session = new OrmliteSession(MyApplication.dataHelper);
	}

	// 增加
	/* (non-Javadoc)
	 * @see data.IDataContext#add(T, java.lang.Class, java.lang.Class)
	 */
	public <T, ID> void add(T item, Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		session.save(item, dataClass, idClass);
	}

	// 删除
	/* (non-Javadoc)
	 * @see data.IDataContext#delete(T, java.lang.Class, java.lang.Class)
	 */
	public <T, ID> void delete(T item, Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		session.delete(item, dataClass, idClass);
	}

	// 更新
	/* (non-Javadoc)
	 * @see data.IDataContext#update(T, java.lang.Class, java.lang.Class)
	 */
	public <T, ID> void update(T item, Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		session.update(item, dataClass, idClass);

	}

	// 根据ID查询
	/* (non-Javadoc)
	 * @see data.IDataContext#queryById(java.lang.Class, java.lang.Class, ID)
	 */
	public <T, ID, K extends ID> T queryById(Class<T> dataClass,
			Class<K> idClass, ID id) throws SQLException {
		return session.queryById(dataClass, idClass, id);
	}

	// 查询全部
	/* (non-Javadoc)
	 * @see data.IDataContext#queryForAll(java.lang.Class, java.lang.Class)
	 */
	public <T, ID> List<T> queryForAll(Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		return session.queryForAll(dataClass, idClass);
	}

	// 根据条件查询
	/* (non-Javadoc)
	 * @see data.IDataContext#queryBySql(java.lang.Class, java.lang.Class, java.lang.String)
	 */
	public <T, ID> List<String[]> queryBySql(Class<T> dataClass,
			Class<ID> idClass, String query) throws SQLException {
		return session.queryBySql(dataClass, idClass, query);
	}

	// 获取全部记录数
	/* (non-Javadoc)
	 * @see data.IDataContext#countof(java.lang.Class, java.lang.Class)
	 */
	public <T, ID> long countof(Class<T> dataClass, Class<ID> idClass)
			throws SQLException {
		return session.countof(dataClass, idClass);
	}

	// 获取符合条件的记录数
	/* (non-Javadoc)
	 * @see data.IDataContext#countof(java.lang.Class, java.lang.Class, com.j256.ormlite.stmt.PreparedQuery)
	 */
	public <T, ID> long countof(Class<T> dataClass, Class<ID> idClass,
			PreparedQuery<T> query) throws SQLException {
		return session.countof(dataClass, idClass, query);
	}

	/* (non-Javadoc)
	 * @see data.IDataContext#query(java.lang.Class, java.lang.Class, com.j256.ormlite.stmt.PreparedQuery)
	 */
	public <T, ID> List<T> query(Class<T> dataClass, Class<ID> idClass,
			PreparedQuery<T> query) throws SQLException {
		return session.query(dataClass, idClass, query);
	}

	/* (non-Javadoc)
	 * @see data.IDataContext#getQueryBuilder(java.lang.Class, java.lang.Class)
	 */
	public <T, ID> QueryBuilder<T, ID> getQueryBuilder(Class<T> dataClass,
			Class<ID> idClass) throws SQLException {

		return session.getQueryBuilder(dataClass, idClass);
	}

	// //获取指定时间段的运动记录
	// public <T, ID> List<String[]> queryByDate(Class<T> dataClass, Class<ID>
	// idClass,
	// Date from, Date to) throws SQLException {
	// return session.queryByDate(dataClass, idClass, from, to);
	// }

	/* (non-Javadoc)
	 * @see data.IDataContext#beginTransaction()
	 */
	public void beginTransaction() {
		session.beginTransaction();
	}

	// 提交事务
	/* (non-Javadoc)
	 * @see data.IDataContext#commit()
	 */
	public void commit() {
		session.commit();
	}

	// 回滚事务
	/* (non-Javadoc)
	 * @see data.IDataContext#rollback()
	 */
	public void rollback() {
		session.rollback();
	}
}
